//
//  VisitorView.swift
//  chaorenWeibo
//
//  Created by tanchao on 15/7/29.
//  Copyright © 2015年 chaoren. All rights reserved.
//

import UIKit
//MARK:- 代理
protocol VisitorViewDelegate:NSObjectProtocol{
    func visitorViewLoginBtnClick()
    func visitorViewRegistorBtnClick()
}
// MARK:- 视图信息
class VisitorView: UIView {
//    设置代理属性
    weak var delegate:VisitorViewDelegate?
//    MARK:- 设置界面信息
    func setupVisitorViewInfo(isHome:Bool,imageName:String,message:String){
        homeIconView.hidden = !isHome
//        maskIconView.hidden = !isHome
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        
    }
//    MARK:- 监听按钮点击事件
     func loginBtnClick()
    {
        delegate?.visitorViewLoginBtnClick()
    }
     func registorBtnClick()
    {
        delegate?.visitorViewRegistorBtnClick()
    }
//    MARK:- 界面初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    // MARK:- 添加控件
    func setUI(){
        /// 添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        addConstraint()
        /// 添加背景色
        backgroundColor = UIColor(red: 237/255.0, green: 237/255.0, blue: 237/255.0, alpha: 1)
    }
    // MARK:- 首页动画
    private func startAnimation(){
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20.0
        animation.removedOnCompletion = false
        iconView.layer.addAnimation(animation, forKey: nil)
        
    }

    //MARK:-  懒加载图标
    lazy private  var iconView: UIImageView = {
        let object = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        return object
        }()
    //MARK:-  懒加载小房子
    lazy private  var homeIconView: UIImageView = {
        let object = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        return object
        }()
    //MARK:-  懒加载消息Label
    private lazy var messageLabel:UILabel = {
        let label = UILabel();
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.grayColor()
//        label.textAlignment = 
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    //MARK:-  懒加载注册按钮
    private lazy var registerButton : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "registorBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //MARK:-  懒加载登录按钮
    private lazy var loginButton : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登陆", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "loginBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    //MARK:-  懒加载遮盖
    private lazy var maskIconView : UIImageView = {
        let mask = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return mask
    }()

    //MARK:- 设置约束
    func addConstraint() {
        /// 图标的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        /// 小房子的约束
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: homeIconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: homeIconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        /// 消息label的约束
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        /// 注册按钮的约束
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 15))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        /// 登录按钮
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -0))
        addConstraint( NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        /// maskIconView
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["maskIconView" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(-35)-[registButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics:nil, views:  ["maskIconView" : maskIconView,"registButton":registerButton]))
    }

    
    
}
